﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NHibernate;
using ZLCMS.Data.Model;
using ZLCMS.Business.Util;
using ZLCMS.Business.DwzFrame;

namespace ZLCMS.Web.SystemManage.weixin.Config
{
    public partial class AccountConfig : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    SysUserInfo user = null;

                    try
                    {
                        user = session.Get<SysUserInfo>(Int32.Parse(Request["uid"]));
                    }
                    catch (System.Exception ex)
                    {

                    } 

                    string account = "";
                    if (user != null)
                    {
                        account = user.UserAccount;
                    }
                    else
                    {
                        account = UserKit.GetUserAccountFailLogin();
                    }

                    WxConfig config = session.CreateQuery("from WxConfig where OwnerAccount=:acc")
                        .SetParameter("acc", account)
                        .UniqueResult<WxConfig>();

                    if (config ==null)
                    {
                        config = new WxConfig();
                    }
                    if (string.IsNullOrEmpty(config.InterfaceToken))
                    {
                        config.InterfaceToken = GenerateToken();
                    }
                    config.InterfaceUrl = ServerUtil.GetRootURI() + "/Interface/weixin/PlatService.aspx?account=" + account;
                        
                    wxaccount.Text = config.WxAccount;
                    token.Text = config.InterfaceToken;
                    url.Text = config.InterfaceUrl;
                    appsecret.Text = config.AppSecret;
                    appid.Text = config.AppId;

                    ViewState["account"] = account;
                }

                //repeater1.DataBind();
            }
            else
            {

                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    WxConfig config = session.CreateQuery("from WxConfig where OwnerAccount=:acc")
                        .SetParameter("acc", ViewState["account"])
                        .UniqueResult<WxConfig>();


                    if (config == null)
                    {
                        config = new WxConfig();
                    }
                    config.InterfaceUrl = ServerUtil.GetRootURI() + "/wxInterface/" + ViewState["account"] + "/index.xhtml";
 
                    config.WxAccount = wxaccount.Text;
                    config.InterfaceToken = token.Text;
                    config.OwnerAccount = ViewState["account"] as string;
                    config.AppId = appid.Text;
                    config.AppSecret = appsecret.Text;
                    session.SaveOrUpdate(config);
                    session.Flush();
                    new FrameHelper(this).Alert(new ReturnCode()
                    {
                        statusCode = 200,
                        message = "操作成功"
                    });
                }

            }
        }

        private string GenerateToken()
        {
            int number;
            char code;
            string checkCode = String.Empty;
            Random random = new Random();

            for (int i = 0; i < 3; i++)
            {
                number = random.Next();
                code = (char)('a' + (char)(number % 26));
                checkCode += code.ToString();
            }

            for (int i = 0; i < 5; i++)
            {
                number = random.Next();
                code = (char)('0' + (char)(number % 10));
                checkCode += code.ToString();
            }
            return checkCode;
        }

    }
}